package cn.tedu.knows.gateway.controller;

import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;

@Controller
public class HomeController {

    //声明两个角色常量用于判断
    public static final GrantedAuthority STUDENT=
            new SimpleGrantedAuthority("ROLE_STUDENT");
    public static final GrantedAuthority TEACHER=
            new SimpleGrantedAuthority("ROLE_TEACHER");

    @GetMapping(value = {"/","/index.html"})
    public String index(
            @AuthenticationPrincipal UserDetails user){
        // 如果是学生,跳学生首页
        if(user.getAuthorities().contains(STUDENT)){
            //location.href=
            //下面的返回值表示将页面重定向(跳转)到
            // /index_student.html
            //等价于response.sendRedirect("/index_student.html");
            return "redirect:/index_student.html";
        }else if(user.getAuthorities().contains(TEACHER)){
            //如果是老师跳老师首页
            return "redirect:/index_teacher.html";
        }
        //如果不是老师也不是学生跳转到登录页
        return "redirect:/login.html";
    }

    @GetMapping("/question/detail.html/{id}")
    public String detail(
            @PathVariable Integer id,
            @AuthenticationPrincipal UserDetails user){
        if(user.getAuthorities().contains(STUDENT)){
            return "redirect:/question/detail_student.html?"+id;
        }else if(user.getAuthorities().contains(TEACHER)){
            return "redirect:/question/detail_teacher.html?"+id;
        }
        return "redirect:/login.html";
    }




}
